package com.lenovo.smarttraffic.zm.item1;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager;
import com.lenovo.smarttraffic.database.dao.DaoManager_Need;
import com.lenovo.smarttraffic.database.dao.NeedtitleBean;
import com.lenovo.smarttraffic.database.dao.NeedtitleBeanDao;
import com.lenovo.smarttraffic.database.dao.TitleStrBean;
import com.lenovo.smarttraffic.database.dao.TitleStrBeanDao;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.fragment.fgBean.DragReturnBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AddItemActivity extends BaseActivity {


    private RecyclerView rv, rv2;
    private List<String> sub_needList;
    private List<String> sub_my;
    private List<String> EventBusStrList;
    private Sub_MyAdapter sub_myAdapter;
    private Button btn_fish;
    private String TAG = "AddItemActivity";
    private Sub_NeedAdapter sub_needAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false));
        sub_myAdapter = new Sub_MyAdapter(this, sub_my);
        rv.setAdapter(sub_myAdapter);

//      实例化ItemTou
        ItemTouchHelper.Callback callback = new OnItemCallbackHelper(sub_myAdapter);
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(rv);
//      Helper对象,然后添加到RecyclerView
        rv2 = findViewById(R.id.rv2);
        rv2.setLayoutManager(new GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false));
        sub_needAdapter = new Sub_NeedAdapter(this, sub_needList);
        rv2.setAdapter(sub_needAdapter);
        btn_fish = findViewById(R.id.btn_fish);
        btn_fish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initData() {

        TitleStrBeanDao titleStrBeanDao = DaoManager.getInstance().getDaoSession().getTitleStrBeanDao();
        List<TitleStrBean> list = titleStrBeanDao.queryBuilder().list();
        List<String> myArrlist = new ArrayList<>();
        if (list.size() == 0) {
            titleStrBeanDao.insert(new TitleStrBean(null, "推荐"));
            titleStrBeanDao.insert(new TitleStrBean(null, "热点"));
            titleStrBeanDao.insert(new TitleStrBean(null, "科技"));
            titleStrBeanDao.insert(new TitleStrBean(null, "汽车资讯"));
            list = titleStrBeanDao.queryBuilder().list();
        }
        for (int i = 0; i < list.size(); i++) {
            myArrlist.add(list.get(i).getTitle());
        }

        EventBusStrList = myArrlist;
        sub_my = myArrlist;

        DaoManager_Need.getmInstance_Need().init(this);
        NeedtitleBeanDao needtitleBeanDao = DaoManager_Need.getmInstance_Need().getDaoSession_Need().getNeedtitleBeanDao();
        List<NeedtitleBean> list1 = needtitleBeanDao.queryBuilder().list();
        List<String> needArrlist = new ArrayList<>();
        if (list1.size() == 0) {
            needtitleBeanDao.insert(new NeedtitleBean(null, "健康"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "财经"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "教育"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "旅游"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "军事"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "实时路况"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "文化"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "二手车"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "违章资讯"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "娱乐"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "体育"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "视频"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "游戏"));
            needtitleBeanDao.insert(new NeedtitleBean(null, "电影"));
            list1 = needtitleBeanDao.queryBuilder().list();
            Log.e(TAG, "initData: " + "执行了吗");
        }
        for (int i = 0; i < list1.size(); i++) {
            needArrlist.add(list1.get(i).getNeed_title());
        }
        sub_needList = needArrlist;
    }

    //reconstruction  重构
    private void reconstruction(boolean flag) {
//        我的订阅
//        Sub_MyAdapter mySubAdapter = new Sub_MyAdapter(this, sub_my, true);
//        rv.setLayoutManager(new GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false));
//        rv.setAdapter(mySubAdapter);
//        ItemTouchHelper.Callback callback = new OnItemCallbackHelper(mySubAdapter);
//        ItemTouchHelper helper = new ItemTouchHelper(callback);
//        helper.attachToRecyclerView(rv);
//        mySubAdapter.notifyDataSetChanged();
    }


    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "添加订阅");
    }


    public class Sub_MyAdapter extends RecyclerView.Adapter<Sub_MyAdapter.Holder> implements OnItemCallbackListener {

        private List<String> mData;
        private Context mContext;
        private String ssss = "wwww";
        private Boolean flag = false;
        private int HuaPosition;

        public Sub_MyAdapter(Context mContext, List<String> list) {
            this.mContext = mContext;
            mData = list;
        }

        public Sub_MyAdapter(Context mContext, List<String> list, String ssss) {
            this.mContext = mContext;
            mData = list;
            this.ssss = ssss;
        }


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_test, parent, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(final Holder holder, int position) {
            if (ssss.equals("zzzz")) {
                Log.e("asdasdasd", "onBindViewHolder: " + ssss);
                if (position == HuaPosition) {
                    holder.iv.setVisibility(View.VISIBLE);
                } else {
                    holder.iv.setVisibility(View.VISIBLE);
                }
            }
            holder.tv.setText(mData.get(position));
            holder.tv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Sub_NeedAdapter needSubAdapter = new Sub_NeedAdapter(mContext, sub_needList, true);
                    rv2.setLayoutManager(new GridLayoutManager(mContext, 5, GridLayoutManager.VERTICAL, false));
                    rv2.setAdapter(needSubAdapter);
                    needSubAdapter.notifyDataSetChanged();
                    Toast.makeText(mContext, "qwertyui", Toast.LENGTH_SHORT).show();
                    ssss = "zzzz";
                    sub_myAdapter.setHua(position);
                    sub_myAdapter.notifyDataSetChanged();
                    btn_fish.setVisibility(View.VISIBLE);
                    return true;
                }
            });
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sub_needList.add(mData.get(position));
                    Sub_NeedAdapter needSubAdapter = new Sub_NeedAdapter(mContext, sub_needList, true);
                    rv2.setLayoutManager(new GridLayoutManager(mContext, 5, GridLayoutManager.VERTICAL, false));
                    rv2.setAdapter(needSubAdapter);
                    needSubAdapter.notifyDataSetChanged();

                    Toast.makeText(mContext, "qwertyui", Toast.LENGTH_SHORT).show();
                    mData.remove(position);
                    ssss = "zzzz";
                    sub_myAdapter.setHua(position);
                    sub_myAdapter.notifyDataSetChanged();
                }
            });
        }

        public void setHua(int HuaPosition) {
            this.HuaPosition = HuaPosition;
        }


        @Override
        public int getItemCount() {
            return mData.size();
        }

        @Override
        public void onMove(int fromPosition, int toPosition) {
            /**
             * 在这里进行给原数组数据的移动
             */
            Collections.swap(mData, fromPosition, toPosition);
            EventBusStrList = mData;
            /**
             * 通知数据移动
             **/
            notifyItemMoved(fromPosition, toPosition);
        }

        @Override
        public void onSwipe(int position) {
            /**
             * 原数据移除数据
             */
            mData.remove(position);
            /**
             * 通知移除
             */
            notifyItemRemoved(position);
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv;
            ImageView iv;

            public Holder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv);
                iv = itemView.findViewById(R.id.iv_reduce_show);
            }
        }
    }


    public class Sub_NeedAdapter extends RecyclerView.Adapter<Sub_NeedAdapter.Holder> {

        private List<String> mData;
        private Context mContext;
        private Boolean flag = false;

        public Sub_NeedAdapter(Context mContext, List<String> list) {
            this.mContext = mContext;
            mData = list;
        }

        public Sub_NeedAdapter(Context mContext, List<String> list, Boolean flag) {
            this.mContext = mContext;
            this.mData = list;
            this.flag = flag;
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_test2, parent, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            holder.tv.setText(mData.get(position));
            if (flag) {
                holder.iv.setVisibility(View.VISIBLE);
            } else {
                holder.iv.setVisibility(View.GONE);
            }
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sub_my.add(mData.get(position));
                    Sub_MyAdapter sub_myAdapter = new Sub_MyAdapter(mContext, sub_my, "zzzz");
                    rv.setLayoutManager(new GridLayoutManager(mContext, 5, GridLayoutManager.VERTICAL, false));
                    rv.setAdapter(sub_myAdapter);
                    sub_myAdapter.notifyDataSetChanged();

                    Toast.makeText(mContext, "qwertyui", Toast.LENGTH_SHORT).show();

                    //  mData.remove(position);
                    sub_needList.remove(position);
                    Sub_NeedAdapter sub_NeedAdapter = new Sub_NeedAdapter(mContext, sub_needList, true);
                    rv2.setLayoutManager(new GridLayoutManager(mContext, 5, GridLayoutManager.VERTICAL, false));
                    rv2.setAdapter(sub_NeedAdapter);
                    sub_NeedAdapter.notifyDataSetChanged();
                }
            });
        }


        @Override
        public int getItemCount() {
            return mData.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv;
            ImageView iv;

            public Holder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv);
                iv = itemView.findViewById(R.id.iv_add_show);
            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        DaoManager.getInstance().init(this);
        TitleStrBeanDao titleStrBeanDao = DaoManager.getInstance().getDaoSession().getTitleStrBeanDao();
        titleStrBeanDao.deleteAll();
        for (int i = 0; i < EventBusStrList.size(); i++) {
            titleStrBeanDao.insert(new TitleStrBean(null, EventBusStrList.get(i)));
        }


        DaoManager_Need.getmInstance_Need().init(this);
        NeedtitleBeanDao needtitleBeanDao = DaoManager_Need.getmInstance_Need().getDaoSession_Need().getNeedtitleBeanDao();
        needtitleBeanDao.deleteAll();
        for (int i = 0; i < sub_needList.size(); i++) {
            needtitleBeanDao.insert(new NeedtitleBean(null, sub_needList.get(i)));
        }
        //更改tabLayout数据
        EventBus.getDefault().post(new DragReturnBean(EventBusStrList));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_add_subscribe;
    }

    public interface OnItemCallbackListener {
        /**
         * @param fromPosition 起始位置
         * @param toPosition   移动的位置
         */
        void onMove(int fromPosition, int toPosition);
        void onSwipe(int position);
    }


}
